package com.virus.mall.service.impl;

import com.virus.mall.dao.AdminDao;
import com.virus.mall.model.Admin;
import com.virus.mall.model.AdminChange;
import com.virus.mall.model.bo.*;
import com.virus.mall.service.AdminService;
import com.virus.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Classname AdminServiceImpl
 * @Description NULL
 * @Date 2021/4/13 14:16
 * @Created by virus
 */
public class AdminServiceImpl implements AdminService {
    public int login(AdminLoginBo adminLoginBo) {
        SqlSession session = MybatisUtils.getSession();
        AdminDao mapper = session.getMapper(AdminDao.class);
        Admin admin = new Admin();
        admin.setEmail(adminLoginBo.getEmail());
        admin.setPwd(adminLoginBo.getPwd());
        int login = mapper.login(admin);
        MybatisUtils.releaseSession(session);
        if (login == 1) {
            return 200;
        }
        return 404;
    }

    public List<Admin> allAdmins() {
        SqlSession session = MybatisUtils.getSession();
        AdminDao mapper = session.getMapper(AdminDao.class);
        List<Admin> adminList = mapper.allAdmins();
        MybatisUtils.releaseSession(session);
        return adminList;
    }

    public List<Admin> getSearchAdmins(AdminSearchBo researchBo) {
        SqlSession session = MybatisUtils.getSession();
        AdminDao mapper = session.getMapper(AdminDao.class);
        Admin admin = new Admin();
        admin.setNickname(researchBo.getNickname());
        admin.setEmail(researchBo.getEmail());
        List<Admin> adminList = mapper.getSearchAdmins(admin);
        MybatisUtils.releaseSession(session);
        return adminList;
    }

    public Admin getAdminsInfo(String id) {
        SqlSession session = MybatisUtils.getSession();
        AdminDao mapper = session.getMapper(AdminDao.class);
        Admin admin = mapper.getAdminsInfo(Integer.parseInt(id));
        MybatisUtils.releaseSession(session);
        return admin;
    }

    public boolean deleteAdmins(String id) {
        SqlSession session = MybatisUtils.getSession();
        AdminDao mapper = session.getMapper(AdminDao.class);
        Integer affectedRow = mapper.deleteAdmins(Integer.parseInt(id));
        MybatisUtils.releaseSession(session);
        return affectedRow > 0;
    }

    public boolean changePwd(AdminChangeBo adminChangeBo) {
        String adminToken = adminChangeBo.getAdminToken();
        String oldPwd = adminChangeBo.getOldPwd();
        String newPwd = adminChangeBo.getNewPwd();
        String confirmPwd = adminChangeBo.getConfirmPwd();
        if (!confirmPwd.equals(newPwd)) {
            return false;
        }
        SqlSession session = MybatisUtils.getSession();
        AdminDao mapper = session.getMapper(AdminDao.class);
        AdminChange adminChange = new AdminChange();
        adminChange.setAdminToken(adminToken);
        adminChange.setOldPwd(oldPwd);
        adminChange.setNewPwd(newPwd);
        Integer affectedRow = mapper.changePwd(adminChange);
        MybatisUtils.releaseSession(session);
        return affectedRow > 0;
    }

    public boolean addAdminss(AdminAddBo adminAddBo) {
        SqlSession session = MybatisUtils.getSession();
        AdminDao mapper = session.getMapper(AdminDao.class);
        Admin admin = new Admin(null, adminAddBo.getEmail(), adminAddBo.getPwd(), adminAddBo.getNickname());
        Integer affectedRow = 0;
        try {
            affectedRow = mapper.addAdminss(admin);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            return false;
        } finally {
            session.close();
        }
        return affectedRow > 0;
    }

    public boolean updateAdminss(AdminUpdateBo adminUpdateBo) {
        SqlSession session = MybatisUtils.getSession();
        AdminDao mapper = session.getMapper(AdminDao.class);
        Admin admin = new Admin(Integer.parseInt(adminUpdateBo.getId()), adminUpdateBo.getEmail(), adminUpdateBo.getPwd(), adminUpdateBo.getNickname());
        Integer affectedRow = 0;
        try {
            affectedRow = mapper.updateAdminss(admin);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            return false;
        }finally {
            session.close();
        }
        return affectedRow > 0;
    }
}
